package br.com.main.client.system.botoes;

import java.util.List;

import br.com.main.client.ComunServico;
import br.com.main.client.ComunServicoAsync;
import br.com.main.client.system.botoes.AreaLazerView.Lazer;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.gwt.view.client.SelectionChangeEvent.Handler;



public class AreaLazerPresenter {

	
	private Display display;

	interface Display {
		
		Button getBtSalvar();
		
		Label getLbNovaAreaLazer();

		void atualizaTabela();

		String[] getDados();

		List<Lazer> getLazerList();
		
		SingleSelectionModel<Lazer> getSelectionModel();

		void enabledOrDesenable(boolean formato);

		TextBox getTbNome();

		TextBox getTbCapacidade();

		TextBox getTbTempo();

		void clean();
	}
	
	public AreaLazerPresenter(Display display) {
		this.display = display;
		bind();
		carregaAreasLazer();
		
	}
	

	private void carregaAreasLazer() {
		getServico().carregarAreaLazer(new AsyncCallback<List<String[]>>() {
			
			@Override
			public void onSuccess(List<String[]> arg0) {
				
				for (String[] strings : arg0) {
					Lazer lazer = new Lazer(strings[2], strings[1], strings[0]);
					display.getLazerList().add(lazer);
				}

				display.atualizaTabela();
				
			}
			
			@Override
			public void onFailure(Throwable arg0) {
				
			}
		});
	}


	private void bind() {
		
		display.getBtSalvar().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				
				getServico().cadastroAreaLazer(display.getDados()[0], display.getDados()[1], display.getDados()[2], new AsyncCallback<String>() {
					
					@Override
					public void onSuccess(String arg0) {
						Lazer lazer = new Lazer( display.getDados()[1], display.getDados()[2], display.getDados()[0]);
						
						display.getLazerList().add(lazer);
						display.atualizaTabela();
						display.clean();
						display.enabledOrDesenable(false);
						
					}
					
					@Override
					public void onFailure(Throwable arg0) {
						// TODO Auto-generated method stub
						
					}
				});
				
			}
		});
		
		display.getLbNovaAreaLazer().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent arg0) {
				display.clean();
				display.enabledOrDesenable(true);
				
			}
		});
		
		display.getSelectionModel().addSelectionChangeHandler(new Handler() {
			
			@Override
			public void onSelectionChange(SelectionChangeEvent arg0) {
				Lazer lazer  = display.getSelectionModel().getSelectedObject();
				
				display.getTbCapacidade().setValue(lazer.getCapacidade());
				display.getTbNome().setValue(lazer.getNome());
				display.getTbTempo().setValue(lazer.getTempoMaximo());
				
			}
		});
	}
	
	private ComunServicoAsync getServico() {
		return GWT.create(ComunServico.class);
	}
}
